/*myData:[{unit:'Đĩa', price: 2300, currency_unit:'VND'},
 {unit:'Kg', price: 3500, currency_unit:'USD'}],*/
(function($){
    var AdminPriceEdit = function(element, options)
    {
        var elem = $(element);
        var obj = this;
        var settings = $.extend({
            myData    : null,
            unitJson    : null,
            currentUnitJson    : null
        }, options || {});



        var rdmStr = $.randomString();
        var txtAddPrice;
        var cboAddCurrencyUnit;
        var cboAddUnit;

        containerHtml = '<div class="ctn_add_price">'+
            '<div class="ctn_add_ctrl">'+
            '<a id="lnkAddPrice" href="javascript:void(0);">'+
            '<span class="edit-add" style="color: #0077B5"><i class="fa fa-plus"></i> Thêm giá</span>'+
            '</a>'+
            '</div>'+
            '<div class="ctn_add_content_ctrl" style="display: none">'+
            '<div>'+
            '<input id="txtAddPrice"  name="txtAddPrice" type="number" value="10000" min="1000" max="99000000"'+
            'placeholder="Nhập giá, bắt buộc"  required validationMessage="Chưa nhập giá" />'+
            '<span class="k-invalid-msg" data-for="txtAddPrice"></span>'+
            '&nbsp;<select id="cboAddCurrencyUnit"></select>'+
            '&nbsp;<select id="cboAddUnit"></select>'+

            '</div>'+
            '<div  class="btn_group" >'+
            '<a id="lnkSaveAddPrice" href="javascript:void(0)">'+
            '<span class="edit-add" style="color: #0077B5"><i class="fa fa-check"></i> Lưu</span>'+
            '</a>'+

            '<a id="lnkCancelAddPrice" href="javascript:void(0);">'+
            '<span class="edit-add" style="color: #0077B5"><i class="fa fa-ban"></i> Cancel</span>'+
            '</a>'+
            '</div>'+
            '</div>'+

            '<div class="ctn_edit_ctrl">'+

            '</div></div>';
        $(elem).html(containerHtml);

        $("#txtAddPrice").width(100).kendoNumericTextBox({
            format: "n0",
            step: 10000
        });
        txtAddPrice= $("#txtAddPrice").data("kendoNumericTextBox");

        $("#cboAddCurrencyUnit").width(80).kendoComboBox({
            dataTextField: "key",
            dataValueField: "val",
            dataSource: settings.currentUnitJson
        });
        cboAddCurrencyUnit= $("#cboAddCurrencyUnit").data("kendoComboBox");

        $("#cboAddUnit").kendoComboBox({
            dataTextField: "name",
            dataValueField: "id",
            dataSource: settings.unitJson
        });

        cboAddUnit= $("#cboAddUnit").data("kendoComboBox");

        $('#lnkAddPrice').click(function(){
            $('.ctn_add_price .ctn_add_ctrl').hide();
            $('.ctn_add_price .ctn_add_content_ctrl').show();
        });

        $('#lnkCancelAddPrice').click(function(){
            $('.ctn_add_price .ctn_add_ctrl').show();
            $('.ctn_add_price .ctn_add_content_ctrl').hide();
        });


        $('#lnkSaveAddPrice').click(function(){
            price = txtAddPrice.value();
            currentUnit = cboAddCurrencyUnit.value();
            unit = cboAddUnit.text();
            aRdmStr =  $.randomString();
            html = '<div class="price_item">'+
                '<div class="price_item_view">'+
                '<span class="unit">'+unit+'</span>&nbsp;'+
                '<span class="price">'+kendo.toString(price, 'n0')+'</span>&nbsp;'+
                '<span class="currency_unit">'+currentUnit+'</span>&nbsp;'+
                '<a id="lnkEditPrice_'+aRdmStr+'" href="javascript:void(0)"><span>Sửa</span></a>&nbsp;'+
                '<a id="lnkRemovePrice_'+aRdmStr+'" href="javascript:void(0)"><span>Xóa</span></a>'+
                '</div>'+
                '<div class="price_item_edit" style="display: none">'+
                '<div>'+
                '<input id="txtEditPrice_'+aRdmStr+'"  name="txtEditPrice_'+aRdmStr+'" type="number" value="'+price+'" min="1000" max="99000000"'+
                'placeholder="Nhâp giá, bắt buộc"  required validationMessage="Chưa nhập giá" />'+
                '<span class="k-invalid-msg" data-for="txtEditPrice_'+aRdmStr+'"></span>'+
                '&nbsp;<select id="cboEditCurrencyUnit_'+aRdmStr+'"></select>'+
                '&nbsp;<select id="cboEditUnit_'+aRdmStr+'"></select>'+
                '</div>'+
                '<div class="btn_group">'+
                '<a id="lnkSaveEditPrice_'+aRdmStr+'" href="javascript:void(0)">'+
                '<span class="edit-add" style="color: #0077B5"><i class="fa fa-check"></i> Lưu</span>'+
                '</a>'+

                '<a id="lnkCancelEditPrice_'+aRdmStr+'" href="javascript:void(0);">'+
                '<span class="edit-add" style="color: #0077B5"><i class="fa fa-ban"></i> Cancel</span>'+
                '</a>'+
                '</div>'+
                '</div>'+
                '</div>';
            $('.ctn_add_price .ctn_edit_ctrl').append(html);

            $("#txtEditPrice_"+aRdmStr).width(100).kendoNumericTextBox({
                format: "n0",
                step: 10000
            });

            $("#cboEditCurrencyUnit_"+aRdmStr).width(80).kendoComboBox({
                dataTextField: "key",
                dataValueField: "val",
                dataSource: settings.currentUnitJson
            });
           

            $("#cboEditUnit_"+aRdmStr).kendoComboBox({
                dataTextField: "name",
                dataValueField: "id",
                dataSource: settings.unitJson
            });

            $('#lnkEditPrice_' + aRdmStr).on('click',function(){
                myParent = $(this).closest('.price_item_view');
                price = myParent.find('span.price').html();
                unit = myParent.find('span.unit').html();
                currency_unit = myParent.find('span.currency_unit').html();
                rdm = $(this).attr('id').substr('lnkEditPrice_'.length)


                txtEditPrice = $("#txtEditPrice_"+ rdm).data("kendoNumericTextBox");
                dumpValue = txtEditPrice.value();

                txtEditPrice.value(price);

                cboEditCurrencyUnit= $("#cboEditCurrencyUnit_"+ rdm).data("kendoComboBox");
                cboEditCurrencyUnit.value(currency_unit);

                cboEditUnit= $("#cboEditUnit_"+ rdm).data("kendoComboBox");
                cboEditUnit.select(function(dataItem) {
                    return dataItem.name == unit;
                });

                myEdit = myParent.siblings('.price_item_edit');
                myParent.hide();
                myEdit.show();
            });

            $('#lnkRemovePrice_' + aRdmStr).on('click',function(){
                myParent = $(this).closest('.price_item');
                myParent.remove();

            });

            $('#lnkSaveEditPrice_' + aRdmStr).on('click',function(){
                myParent = $(this).closest('.price_item_edit');
                rdm = $(this).attr('id').substr('lnkSaveEditPrice_'.length)

                txtEditPrice= $("#txtEditPrice_"+ rdm).data("kendoNumericTextBox");
                price = txtEditPrice.value();

                cboEditCurrencyUnit= $("#cboEditCurrencyUnit_"+ rdm).data("kendoComboBox");
                currency_unit = cboEditCurrencyUnit.value();

                cboEditUnit= $("#cboEditUnit_"+ rdm).data("kendoComboBox");
                unit = cboEditUnit.text();
                myView = myParent.siblings('.price_item_view');


                myView.find('span.price').html(kendo.toString(price, 'n0'));
                myView.find('span.currency_unit').html(currency_unit);
                myView.find('span.unit').html(unit);
                myView.show();

                myParent.hide();
            })


            $('#lnkCancelEditPrice_' + aRdmStr).on('click',function(){
                rdm = $(this).attr('id').substr('lnkCancelEditPrice_'.length)
                myParent = $(this).closest('.price_item_edit');
                myView = myParent.siblings('.price_item_view');

                price = myView.find('span.price').html();
                unit = myView.find('span.unit').html();
                currency_unit = myView.find('span.currency_unit').html();

                txtEditPrice= $("#txtEditPrice_"+ rdm).data("kendoNumericTextBox");
                txtEditPrice.value(price);

                cboEditCurrencyUnit= $("#cboEditCurrencyUnit_"+ rdm).data("kendoComboBox");
                cboEditCurrencyUnit.value(currency_unit);

                cboEditUnit= $("#cboEditUnit_"+ rdm).data("kendoComboBox");
                cboEditUnit.select(function(dataItem) {
                    return dataItem.name == unit;
                });

                myParent.hide();
                myView.show();
            })

            $('.ctn_add_price .ctn_add_ctrl').show();
            $('.ctn_add_price .ctn_add_content_ctrl').hide();
        });
        if(settings.myData){
            for(var i = 0; i < settings.myData.length; i++) {
                aPriceObj = settings.myData[i];
                aRdmStr =  $.randomString();
                html = '<div class="price_item">'+
                    '<div class="price_item_view">'+
                    '<span class="unit">'+aPriceObj.unit+'</span>&nbsp;'+
                    '<span class="price">'+ kendo.toString(aPriceObj.price, 'n0')+'</span>&nbsp;'+
                    '<span class="currency_unit">'+aPriceObj.currency_unit+'</span>&nbsp;'+
                    '<a id="lnkEditPrice_'+aRdmStr+'" href="javascript:void(0)"><span>Sửa</span></a>&nbsp;'+
                    '<a id="lnkRemovePrice_'+aRdmStr+'" href="javascript:void(0)"><span>Xóa</span></a>'+
                    '</div>'+
                    '<div class="price_item_edit" style="display: none">'+
                    '<div>'+
                    '<input id="txtEditPrice_'+aRdmStr+'"  name="txtEditPrice_'+aRdmStr+'" type="number" value="'+aPriceObj.price+'" min="1000" max="99000000"'+
                    'placeholder="Nhâp giá, bắt buộc"  required validationMessage="Chưa nhập giá" />'+
                    '<span class="k-invalid-msg" data-for="txtEditPrice_'+aRdmStr+'"></span>'+
                    '&nbsp;<select id="cboEditCurrencyUnit_'+aRdmStr+'"></select>'+
                    '&nbsp;<select id="cboEditUnit_'+aRdmStr+'"></select>'+
                    '</div>'+
                    '<div class="btn_group">'+
                    '<a id="lnkSaveEditPrice_'+aRdmStr+'" href="javascript:void(0)">'+
                    '<span class="edit-add" style="color: #0077B5"><i class="fa fa-check"></i> Lưu</span>'+
                    '</a>'+

                    '<a id="lnkCancelEditPrice_'+aRdmStr+'" href="javascript:void(0);">'+
                    '<span class="edit-add" style="color: #0077B5"><i class="fa fa-ban"></i> Cancel</span>'+
                    '</a>'+
                    '</div>'+
                    '</div>'+
                    '</div>';
                $('.ctn_add_price .ctn_edit_ctrl').append(html);


                $("#txtEditPrice_"+aRdmStr).width(100).kendoNumericTextBox({
                    format: "n0",
                    step: 10000
                });

                $("#cboEditCurrencyUnit_"+aRdmStr).width(80).kendoComboBox({
                    dataTextField: "key",
                    dataValueField: "val",
                    dataSource: settings.currentUnitJson
                });
              


                $("#cboEditUnit_"+aRdmStr).kendoComboBox({
                    dataTextField: "name",
                    dataValueField: "id",
                    dataSource: settings.unitJson
                });

                $('#lnkEditPrice_' + aRdmStr).on('click',function(){
                    myParent = $(this).closest('.price_item_view');
                    price = myParent.find('span.price').html();
                    unit = myParent.find('span.unit').html();
                    currency_unit = myParent.find('span.currency_unit').html();
                    rdm = $(this).attr('id').substr('lnkEditPrice_'.length)


                    txtEditPrice = $("#txtEditPrice_"+ rdm).data("kendoNumericTextBox");
                    dumpValue = txtEditPrice.value();

                    txtEditPrice.value(price);

                    cboEditCurrencyUnit= $("#cboEditCurrencyUnit_"+ rdm).data("kendoComboBox");
                    cboEditCurrencyUnit.value(currency_unit);

                    cboEditUnit= $("#cboEditUnit_"+ rdm).data("kendoComboBox");
                    cboEditUnit.select(function(dataItem) {
                        return dataItem.name == unit;
                    });

                    myEdit = myParent.siblings('.price_item_edit');
                    myParent.hide();
                    myEdit.show();
                });

                $('#lnkRemovePrice_' + aRdmStr).on('click',function(){
                    myParent = $(this).closest('.price_item');
                    myParent.remove();

                });

                $('#lnkSaveEditPrice_' + aRdmStr).on('click',function(){
                    myParent = $(this).closest('.price_item_edit');
                    rdm = $(this).attr('id').substr('lnkSaveEditPrice_'.length)

                    txtEditPrice= $("#txtEditPrice_"+ rdm).data("kendoNumericTextBox");
                    price = txtEditPrice.value();

                    cboEditCurrencyUnit= $("#cboEditCurrencyUnit_"+ rdm).data("kendoComboBox");
                    currency_unit = cboEditCurrencyUnit.value();

                    cboEditUnit= $("#cboEditUnit_"+ rdm).data("kendoComboBox");
                    unit = cboEditUnit.text();
                    myView = myParent.siblings('.price_item_view');


                    myView.find('span.price').html(kendo.toString(price, 'n0'));
                    myView.find('span.currency_unit').html(currency_unit);
                    myView.find('span.unit').html(unit);
                    myView.show();

                    myParent.hide();
                })


                $('#lnkCancelEditPrice_' + aRdmStr).on('click',function(){
                    rdm = $(this).attr('id').substr('lnkCancelEditPrice_'.length)
                    myParent = $(this).closest('.price_item_edit');
                    myView = myParent.siblings('.price_item_view');

                    price = myView.find('span.price').html();
                    unit = myView.find('span.unit').html();
                    currency_unit = myView.find('span.currency_unit').html();

                    txtEditPrice= $("#txtEditPrice_"+ rdm).data("kendoNumericTextBox");
                    txtEditPrice.value(price);

                    cboEditCurrencyUnit= $("#cboEditCurrencyUnit_"+ rdm).data("kendoComboBox");
                    cboEditCurrencyUnit.value(currency_unit);

                    cboEditUnit= $("#cboEditUnit_"+ rdm).data("kendoComboBox");
                    cboEditUnit.select(function(dataItem) {
                        return dataItem.name == unit;
                    });

                    myParent.hide();
                    myView.show();
                })

            }
        }

        // Public method - can be called from client code
        this.getPrices = function()
        {
            resultArr = [];

            elem.find('.price_item_view ').each(function(){
                unit = $(this).find('span.unit').html();
                price = $(this).find('span.price').html();
                currency_unit = $(this).find('span.currency_unit').html();
                tmpJson = {};
                tmpJson['unit'] =  unit;
                tmpJson['price'] =  price;
                tmpJson['currency_unit'] =  currency_unit;
                resultArr.push(tmpJson);

            })

            return resultArr;
        };

        // Private method - can only be called from within this object
        var privateMethod = function()
        {
            console.log('private method called!');
        };
    };

    $.fn.adminPriceEdit = function(options)
    {
        return this.each(function()
        {
            var element = $(this);

            // Return early if this element already has a plugin instance
            if (element.data('adminPriceEdit')) return;

            // pass options to plugin constructor
            var adminPriceEdit = new AdminPriceEdit(this, options);

            // Store plugin object in this element's data
            element.data('adminPriceEdit', adminPriceEdit);



        });
    };
})(jQuery);
